import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class GameFrame extends JFrame {
    GamePanel panel;

    public GameFrame() {
        panel = new GamePanel();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.pack();
        this.setLocationRelativeTo(null); // 窗口居中
        this.setVisible(true);
        this.addKeyListener(new GameKeyListener(panel));
        panel.requestFocusInWindow(); // 确保panel获取到焦点
    }
}

class GamePanel extends JPanel {
    int x = 150, y = 150; // 赛车的起始位置
    int obstacleX = 150, obstacleY = -100; // 障碍物的起始位置
    int speed = 2; // 障碍物的移动速度
    int score = 0; // 玩家得分

    public GamePanel() {
        this.setPreferredSize(new Dimension(400, 400));
        this.setFocusable(true); // 允许获取焦点

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_RIGHT -> x += 10;
                    case KeyEvent.VK_LEFT -> x -= 10;
                    case KeyEvent.VK_UP -> y -= 10;
                    case KeyEvent.VK_DOWN -> y += 10;
                }
                repaint();
            }
        });

        this.requestFocusInWindow(); // 请求焦点
        Timer timer = new Timer(20, e -> gameUpdate());
        timer.start();
    }

    private void gameUpdate() {
        obstacleY += speed; // 障碍物向下移动
        if (obstacleY > this.getHeight()) {
            obstacleY = -100;
            obstacleX = new Random().nextInt(this.getWidth() - 30); // 障碍物重新出现在顶部的随机位置
            score++; // 每次避开障碍物得分增加
        }
        checkCollision();
        repaint();
    }

    private void checkCollision() {
        Rectangle carRect = new Rectangle(x, y, 50, 30);
        Rectangle obstacleRect = new Rectangle(obstacleX, obstacleY, 30, 30);
        if (carRect.intersects(obstacleRect)) {
            JOptionPane.showMessageDialog(this, "游戏结束！你的得分是：" + score);
            System.exit(0); // 或者重置游戏状态而不是退出
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制背景和赛车
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.RED);
        g.fillRect(x, y, 50, 30);

        // 绘制障碍物
        g.setColor(Color.BLACK);
        g.fillRect(obstacleX, obstacleY, 30, 30);

        // 显示得分
        g.setColor(Color.BLACK);
        g.drawString("得分：" + score, 10, 20);
    }

    public void moveCar(int dx, int dy) {
        x += dx;
        y += dy;
        this.repaint();
    }
}

class GameKeyListener implements KeyListener {
    GamePanel panel;

    public GameKeyListener(GamePanel panel) {
        this.panel = panel;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // 故意留空，以满足KeyListener接口的实现要求
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int dx = 0, dy = 0;
        switch (e.getKeyCode()) {
            case KeyEvent.VK_RIGHT -> dx = 10;
            case KeyEvent.VK_LEFT -> dx = -10;
            case KeyEvent.VK_UP -> dy = -10;
            case KeyEvent.VK_DOWN -> dy = 10;
        }
        panel.moveCar(dx, dy);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // 故意留空，以满足KeyListener接口的实现要求
    }
}